<?PHP
class Form_Business extends Zend_Form
{  
	public function __construct($businessId="")
	{
		global $mySession;
		$this->init($businessId);
	}
    public function init($businessId)
	{   
		global $mySession;
        $db=new Db();
		$business_title_value="";$business_category_id_value="";$description_value="";$search_keywords_value="";$address_value="";$city_name_value="";$zipcode_value="";$state_id_value="";$country_id_value="218";$phone_number_value="";$email_address_value="";$website_value="";$old_business_images_path="";
		
		if($businessId!="")
		{
		$businessData=$db->runQuery("select * from ".SERVICE_BUSINESS." where business_id='".$businessId."'");
		$business_title_value=$businessData[0]['business_title'];
		$business_category_id_value=$businessData[0]['business_category_id'];
		$description_value=$businessData[0]['description'];
		$search_keywords_value=$businessData[0]['search_keywords'];
		$address_value=$businessData[0]['address'];
		$city_name_value=$businessData[0]['city_name'];
		$zipcode_value=$businessData[0]['zipcode'];
		$state_id_value=$businessData[0]['state_id'];
		$country_id_value=$businessData[0]['country_id'];
		$phone_number_value=$businessData[0]['phone_number'];
		$email_address_value=$businessData[0]['email_address'];
		$website_value=$businessData[0]['website'];
		$old_business_images_path=$businessData[0]['business_image'];
		}		
		
		$business_title= new Zend_Form_Element_Text('business_title');
		$business_title->setRequired(true)
		->addValidator('NotEmpty',true,array('messages' =>'Business title is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($business_title_value);
		$this->addElement($business_title);
		
		$CategoryArr=array();
		$CategoryArr[0]['key']="";
		$CategoryArr[0]['value']="- - Category - -";
		$CategoryData=$db->runQuery("select * from ".SERVICE_CATEGORIES." order by category_name");
		if($CategoryData!="" and count($CategoryData)>0)
		{
			$i=1;
			foreach($CategoryData as $key=>$CategoryValues)
			{
			$CategoryArr[$i]['key']=$CategoryValues['cat_id'];
			$CategoryArr[$i]['value']=$CategoryValues['category_name'];
			$i++;
			}
		}
		
		$business_category_id= new Zend_Form_Element_Select('business_category_id');
		$business_category_id->setRequired(true)
		->addMultiOptions($CategoryArr)
		->addValidator('NotEmpty',true,array('messages' =>'Business category is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($business_category_id_value);
		$this->addElement($business_category_id);
		
		
		
		$SubscriptionLimitData=$db->runQuery("select des_box,nof_images from ".USER_SUBSCRIPTIONS." 
		join ".SUBSCRIPTIONS." on(".USER_SUBSCRIPTIONS.".plan_id=".SUBSCRIPTIONS.".plan_id)
		where user_id='".$mySession->LoggedUserId."' and is_active='1'");
		
		if($SubscriptionLimitData[0]['des_box']=='1')
		{
		$description= new Zend_Form_Element_Text('description');
		$description->setRequired(true)
		->addValidator('NotEmpty',true,array('messages' =>'Business description is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($description_value);
		$this->addElement($description);
		}
		else
		{
		$description= new Zend_Form_Element_Textarea('description');
		$description->setAttrib("class","textInput")
		->setAttrib("rows","3")
		->setAttrib("style","height:100px;")
		->setRequired(true)
		->addValidator('NotEmpty',true,array('messages' =>'Business description is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setValue($description_value);
		$this->addElement($description);
		}
		
		$search_keywords= new Zend_Form_Element_Textarea('search_keywords');
		$search_keywords->setAttrib("class","textInput")
		->setAttrib("rows","3")
		->setAttrib("style","height:100px;")
		->setValue($search_keywords_value);
		$this->addElement($search_keywords);
		
		$address= new Zend_Form_Element_Text('address');
		$address->setRequired(true)
		->addValidator('NotEmpty',true,array('messages' =>'Location on map is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($address_value);
		$this->addElement($address);
		
		$city_name= new Zend_Form_Element_Text('city_name');
		$city_name->setRequired(true)
		->addValidator('NotEmpty',true,array('messages' =>'City is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($city_name_value);
		$this->addElement($city_name);
		
		$zipcode= new Zend_Form_Element_Text('zipcode');
		$zipcode->setRequired(true)
		->addValidator('NotEmpty',true,array('messages' =>'Zipcode is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($zipcode_value);
		$this->addElement($zipcode);
		
		$StateArr=array();
		$StateArr[0]['key']="";
		$StateArr[0]['value']="- - State - -";
		$chkCountryId=$country_id_value;
		if(isset($_REQUEST['country_id']) && $_REQUEST['country_id']!="")
		{
			$chkCountryId=$_REQUEST['country_id'];
		}
		if($chkCountryId!="")
		{
			$StateData=$db->runQuery("select * from ".STATE." where country_id='".$chkCountryId."' order by state_name");
			if($StateData!="" and count($StateData)>0)
			{
				$i=1;
				foreach($StateData as $key=>$StateValues)
				{
				$StateArr[$i]['key']=$StateValues['state_id'];
				$StateArr[$i]['value']=$StateValues['state_name'];
				$i++;
				}
			}
		}
		$state_id= new Zend_Form_Element_Select('state_id');
		$state_id->setRequired(true)
		->addMultiOptions($StateArr)
		->addValidator('NotEmpty',true,array('messages' =>'State is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($state_id_value);
		$this->addElement($state_id);
		
		$CountryArr=array();
		$CountryArr[0]['key']="";
		$CountryArr[0]['value']="- - Country - -";
		$CountryData=$db->runQuery("select * from ".COUNTRIES." order by country_name");
		if($CountryData!="" and count($CountryData)>0)
		{
			$i=1;
			foreach($CountryData as $key=>$CountryValues)
			{
			$CountryArr[$i]['key']=$CountryValues['country_id'];
			$CountryArr[$i]['value']=$CountryValues['country_name'];
			$i++;
			}
		}
		
		$country_id= new Zend_Form_Element_Select('country_id');
		$country_id->setRequired(true)
		->addMultiOptions($CountryArr)
		->addValidator('NotEmpty',true,array('messages' =>'Country is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setAttrib("onchange","getCountryState(this.value);")	
		->setValue($country_id_value);
		$this->addElement($country_id);
		
		$phone_number= new Zend_Form_Element_Text('phone_number');
		$phone_number->setRequired(true)
		->addValidator('NotEmpty',true,array('messages' =>'Phone number is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($phone_number_value);
		$this->addElement($phone_number);
		
		$email_address= new Zend_Form_Element_Text('email_address');
		$email_address->setRequired(true)
		->addValidator('NotEmpty',true,array('messages' =>'Email address is required.'))
		->addDecorator('Errors', array('class'=>'errmsg'))
		->setAttrib("class","textInput")
		->setValue($email_address_value);
		$this->addElement($email_address);
		
		$website= new Zend_Form_Element_Text('website');
		$website->setAttrib("class","textInput")
		->setValue($website_value);
		$this->addElement($website);
		
		if($old_business_images_path!="")
		{
			$explode=explode(",",$old_business_images_path);	
		}

		for($counter=1;$counter<=$SubscriptionLimitData[0]['nof_images'];$counter++)
		{
		$businessImageName='business_image'.$counter;
		$businessImageName= new Zend_Form_Element_File($businessImageName);
		$businessImageName->setDestination(SITE_ROOT.'images/businesses/')
        ->addValidator('Extension', false, 'jpg,jpeg,png,gif')
		->addDecorator('Errors', array('class'=>'errmsg'));
		$this->addElement($businessImageName);
		
		$businessImagePath='old_business_image'.$counter;
		$businessImagePath= new Zend_Form_Element_Hidden($businessImagePath);
		$businessImagePath->setValue(@$explode[$counter-1]);
		$this->addElement($businessImagePath);		
		}		
	}
}	
?>